package com.wise.service.cms.content.pagedecorates;

import com.wise.entities.content.Decorate;
import com.wise.entities.content.DecorateType;
import com.wise.framework.dao.SqlSelect;
import com.wise.framework.daomodel.DaoPageList;
import com.wise.framework.restful.PageResultList;
import com.wise.summer.v1.cms.content.pagedecorates.ListPageDecorates;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.summerframework.model.SummerService;
import org.summerframework.model.SummerServiceBean;

@Service
@SummerService
public class ListPageDecoratesService implements SummerServiceBean<ListPageDecorates> {

    @Override
    public void sum(ListPageDecorates model) {
        if (model.getIssue() == null){
            model.setIssue(false);
        }
        SqlSelect select = SqlSelect.selec("*");

        select.fromClassName(Decorate.class);
        select.eq("type", DecorateType.PC.value());
        select.eq(!StringUtils.isEmpty(model.getModule()), "module", model.getModule());
        select.eqLang(Decorate._lang);
        select.like(!StringUtils.isEmpty(model.getTitle()), "title", "%" + model.getTitle() + "%");
        select.like(!StringUtils.isEmpty(model.getAlias()), Decorate.ALIAS, "%" + model.getAlias() + "%");
        select.where(model.getIssue(), Decorate._publicContent + " is not null");
        select.order_by(Decorate._lastUpdateDate + " desc");
        select.order_by("id desc");

        PageResultList<Decorate> pageList = DaoPageList.s(Decorate.class, select, model.getStart(), model.getLimit());
        model.setSummerResult(pageList);

    }
}
